package com.eseo.munchkinarbiter;

import java.util.ArrayList;
import java.util.List;

public class GameData {
	private List<PlayerStats> players = new ArrayList<PlayerStats>();
	private FightStats fightStats = new FightStats(this);
	private int currentPlayer = 0;
	private List<GameChangeListener> listeners = new ArrayList<GameChangeListener>();
	
	public GameData() {
	}
	
	public List<PlayerStats> getPlayers() {
		return players;
	}
	
	public void addPlayer(String name) {
		PlayerStats player = new PlayerStats(this, name);
		players.add(player);
		if(players.size() == 1)
			onCurrentPlayerChanged(player);
	}
	
	public PlayerStats getCurrentPlayer() {
		if(currentPlayer >= 0 && currentPlayer < players.size())
			return players.get(currentPlayer);
		else
			return null;
	}
	
	public void nextPlayer() {
		if(currentPlayer >= players.size() - 1)
			currentPlayer = 0;
		else
			currentPlayer++;
		onCurrentPlayerChanged(players.get(currentPlayer));
	}
	
	public void previousPlayer() {
		if(currentPlayer <= 0)
			currentPlayer = players.size() - 1;
		else
			currentPlayer--;
		onCurrentPlayerChanged(players.get(currentPlayer));
	}
	
	public FightStats getFight() {
		return fightStats;
	}
	
	//Events
	public void addListener(GameChangeListener g) {
		listeners.add(g);
	}
	public void removeListener(GameChangeListener g) {
		listeners.remove(g);
	}

	public void onCurrentPlayerChanged(PlayerStats player) {
		for(GameChangeListener g : listeners)
			g.onCurrentPlayerChanged(this, player);
	}
	
	public void onPlayerNameChanged(PlayerStats player) {
		for(GameChangeListener g : listeners)
			g.onPlayerNameChanged(this, player);
	}
	
	public void onWarriorStateChanged(PlayerStats player) {
		for(GameChangeListener g : listeners)
			g.onWarriorStateChanged(this, player);
	}
	
	public void onPlayerLevelChanged(PlayerStats player) {
		for(GameChangeListener g : listeners)
			g.onPlayerLevelChanged(this, player);
	}
	
	public void onPlayerGearChanged(PlayerStats player) {
		for(GameChangeListener g : listeners)
			g.onPlayerGearChanged(this, player);
	}
	
	public void onPlayerStrengthChanged(PlayerStats player) {
		for(GameChangeListener g : listeners)
			g.onPlayerStrengthChanged(this, player);
	}
	
	public void onMonsterLevelChanged() {
		for(GameChangeListener g : listeners)
			g.onMonsterLevelChanged(this);
	}
	
	public void onMonsterModChanged() {
		for(GameChangeListener g : listeners)
			g.onMonsterModChanged(this);
	}
	
	public void onPlayerModChanged() {
		for(GameChangeListener g : listeners)
			g.onPlayerModChanged(this);
	}
	
	public void onHelpPlayerChanged() {
		for(GameChangeListener g : listeners)
			g.onHelpPlayerChanged(this);
	}
	
	public void onFightResultChanged() {
		for(GameChangeListener g : listeners)
			g.onFightResultChanged(this);
	}
}
